<template>
  <div v-show="$route.name == 'export'">
    <!-- 面包屑导航区域 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>下载表格</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片视图区域 -->
    <el-card style="margin-bottom: 15px;" v-if="groupid === 0">
      <div style="margin-bottom: 20px;">进销存报表</div>
      <!-- 搜索与添加区域 -->
      <el-row class="query">
        <el-col :span="6">
          <el-date-picker
            v-model="month"
            type="month"
            :clearable="false"
            value-format='yyyy-MM'
            :picker-options="pickerOptions"
            placeholder="选择月">
          </el-date-picker>
        </el-col>
      </el-row>
      <el-row class="query">
        <el-button type="warning" @click="download(1)" :loading="loading" style="margin-right: 5px;">下载</el-button>
      </el-row>
    </el-card>
    <el-card style="margin-bottom: 15px;" v-if="groupid === 1">
      <div style="margin-bottom: 20px;">当日出仓明细</div>
      <!-- 搜索与添加区域 -->
      <el-row class="query">
        <el-col :span="6">
          <!-- <el-date-picker
            v-model="outDate"
            type="date"
            :clearable="false"
            value-format="yyyy-MM-dd"
            placeholder="选择日期"
            :picker-options="pickerDateOptions">
          </el-date-picker> -->
          <el-date-picker
            v-model="outDate"
            type="daterange"
            :clearable="false"
            :picker-options="pickerDateOptions"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期">
          </el-date-picker>
        </el-col>
      </el-row>
      <el-row class="query">
        <el-button type="warning" @click="download(2)" :loading="loading" style="margin-right: 5px;">导出出仓统计表excel文件</el-button>
      </el-row>
    </el-card>
  </div>
</template>
<script>
import moment from 'moment'
export default {
  data() {
    return {
      groupid: '',
      loading: false,
      month: '',
      pickerOptions: {
        disabledDate(time) {
          let a = moment().subtract(1, 'month').format('YYYY')
          let b = moment().subtract(1, 'month').format('M')
          return time.getMonth() > b || time.getFullYear() != a
        }
      },
      outDate: '',
      pickerDateOptions: {
        disabledDate(time) {
          return time.getTime() < new Date('2022-11-15')
        }
      }
    }
  },
  mounted() {
    this.month = moment().subtract(1, 'month').format('YYYY-MM')
    this.outDate = [moment().format('YYYY-MM-DD'), moment().format('YYYY-MM-DD')]
    this.getUserInfo()
  },
  methods: {
    // 获取用户信息
    async getUserInfo() {
      const { data: result } = await this.$http.get('/user/' + localStorage.userid)
      if (result.status !== 200) return this.$message.error(result.msg)
      this.groupid = result.data.groupid * 1
    },
    async download(type) {
      // 下载进销存表
      if (type === 1) {
        if (this.month == '') {
          return this.$message.error('请选择月份')
        }
        this.loading = true
        const data = {
          month: this.month
        }
        await this.$http.post('/export-inout', data, { type: 'json',responseType:'blob' }).then(res => {
          if (res.status === 200) {
            const blob = res.data
            const reader = new FileReader()
            reader.readAsDataURL(blob)
            reader.onload = (e) => {
              const a = document.createElement('a')
              a.download = '冠记明细-进销存报表.xlsx'
              a.href = e.target.result
              a.click()
            }
            this.loading = false
            return this.$message.success('导出成功')
          }
          this.loading = false
          return this.$message.error(res.msg)
        }).catch (() => {
          this.loading = false
          return this.$message.error('导出失败')
        })
      }
      // 下载当日出仓明细
      if (type === 2) {
        // if (this.outDate == '') {
        //   return this.$message.error('请选择日期')
        // }
        var data = {}
        if (this.outDate) {
          const startTime = new Date(this.outDate[0])
          const syear = startTime.getFullYear() // 获取当前年份
          const smonth = startTime.getMonth() + 1 // 获取当前月份(0-11,0代表1月所以要加1);
          const sday = startTime.getDate() // 获取当前日（1-31）
          const endTime = new Date(this.outDate[1])
          const eyear = endTime.getFullYear() // 获取当前年份
          const emonth = endTime.getMonth() + 1 // 获取当前月份(0-11,0代表1月所以要加1);
          const eday = endTime.getDate() // 获取当前日（1-31）
          if (startTime) {
            data.startTime = `${syear}-${smonth}-${sday}`
          }
          if (endTime) {
            data.endTime = `${eyear}-${emonth}-${eday}`
          }
        }
        this.loading = true
        await this.$http.post('/export-dateout', data, { type: 'json',responseType:'blob' }).then(res => {
          if (res.status === 200) {
            const blob = res.data
            const reader = new FileReader()
            reader.readAsDataURL(blob)
            reader.onload = (e) => {
              const a = document.createElement('a')
              a.download = '冠记明细-当日出仓明细表.xlsx'
              a.href = e.target.result
              a.click()
            }
            this.loading = false
            return this.$message.success('导出成功')
          }
          this.loading = false
          return this.$message.error(res.msg)
        }).catch (() => {
          this.loading = false
          return this.$message.error('导出失败')
        })
      }
    }
  }
}
</script>
<style scoped>
  .query {
    margin-bottom: 10px;
  }
</style>